package com.hmall.common.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
//#条件加载器，当如果类加载器中有rabbitTemplate时，才会加载该依赖
@ConditionalOnClass(RabbitTemplate.class)
public class MQConfig {
    @Bean
    public MessageConverter messageConverter() {
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        jackson2JsonMessageConverter.setCreateMessageIds(true);
        return jackson2JsonMessageConverter;
    }
//    @Bean
//    public TopicExchange exchange() {
//        return new TopicExchange("error.direct");
//    }
//
//    @Bean
//    public Queue queue() {
//        Map<String, Object> arguments = new HashMap<>();
//        // 设置死信交换机
//        arguments.put("x-dead-letter-exchange", "dlx.exchange");
//        // 设置死信路由键
//        arguments.put("x-dead-letter-routing-key", "dlx.order");
//        // 可选：设置消息TTL
//        arguments.put("x-message-ttl", 60000);
//        // 可选：设置队列最大长度
//        arguments.put("x-max-length", 1000);
//
//        return new Queue("normal-queue", true, false, false, arguments);
////        return new Queue("error-queue");
//    }
//    @Bean
//    public Binding binding(Queue queue, TopicExchange exchange){
////        return  new Binding("order-queue", Binding.DestinationType.QUEUE, "order-exchange", "order.delete", null)
//       return BindingBuilder.bind(queue).to( exchange).with("order.delete");
//    }
//    @Bean
//    public MessageRecoverer messageConverter2(RabbitTemplate rabbitTemplate){
//         return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");
//    }
}
